#!/bin/bash
set -u -e -E -o pipefail

CMD=${1:?no command given, aborting}

PROG=/usr/bin/yadtbroadcast-server.py
NAME=$(basename $PROG .py)
PID=$(pidof python | xargs ps -o pid=,cmd= | grep $PROG | sed s/^\\s*// | cut -d" " -f1 || :)
NOLOG=/tmp/$NAME.nolog

case "$CMD" in
    start)
        if [[ -z $PID ]]; then
            echo "starting $PROG"
            exec $PROG < /dev/null > $NOLOG 2>&1 &
        else
            echo "already running"
        fi
        exit 0
        ;;
    stop)
        if [[ -n $PID ]]; then
            echo "stopping $PROG"
            kill $PID
        else
            echo "already stopped"
        fi
        exit 0
        ;;
    status)
        echo "pid of $NAME service: $PID"
        [[ -n $PID ]]
        exit
        ;;
    *)
        ;;
esac

